package com.xpgk.mvc.productPrice;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.dept.DepartmentService;

@Service(name = ProductPriceSetService.serviceName)
public class ProductPriceSetService extends BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(DepartmentService.class);

	public static final String serviceName = "productPriceSetService";
	
	
	/**
	 * 保存
	 * 
	 * @param pIds
	 * @param names
	 * @param orderIds
	 * @return
	 */
	@Before(Tx.class)
	public RenderBean addPrice(List<ProductPrice> productPrices, Price price) {
		RenderBean result = new RenderBean();
		//添加价格表
		price.set(Price.column_addTime, new Date()).save();
		
		//添加产品价格
		for(ProductPrice pp : productPrices){
			pp.set(ProductPrice.column_priceIds, price.getPKValue()).set(ProductPrice.column_starttime, new Date()).save();
		}
		
		result.setCode(ConstantRender.render_success_code);
		return result;
	}
	
	/**
	 * 更新
	 * 
	 * @param ids
	 * @param names
	 */
	@Before(Tx.class)
	public RenderBean updatePrice(List<ProductPrice> productPrices, Price price) {
		RenderBean result = new RenderBean();
		if(null != price.getPKValue()){
			price.update();
			if(productPrices.size() > 0){
				/*List<Organization> organs = Organization.dao.findOrganByPriceIds(price.getPKValue());//获取所有使用指定价格表的机构
				if(organs.size() > 0){
					for(Organization organ : organs){
						List<Price> prices = Price.dao.findByOrgan(organ.getPKValue());//获取所有机构下的价格表
						if(prices.size() > 0){
							for(Price p : prices){
								for(ProductPrice pp : productPrices){
									String sql = "select * from sb_product_price where priceIds=? and productIds=? and status='1'";
									ProductPrice op = ProductPrice.dao.findFirst(sql, p.getPKValue(), pp.getStr(ProductPrice.column_productIds));
									if(null != op){
										//禁用之前价格
										ProductPrice.dao.findById(op.getPKValue()).set(ProductPrice.column_status, 0).set(ProductPrice.column_disabletime, new Date()).update();
									}
									if(pp.getFloat(ProductPrice.column_outPrice)==null||pp.getFloat(ProductPrice.column_outPrice).equals("null")){
										pp.set(ProductPrice.column_outPrice, 0.0);
									}
									new ProductPrice()
									.set(ProductPrice.column_priceIds, p.getPKValue())
									.set(ProductPrice.column_putPrice, pp.getFloat(ProductPrice.column_outPrice))
									.set(ProductPrice.column_productIds, pp.getStr(ProductPrice.column_productIds))
									.set(ProductPrice.column_outPrice, 0.0)
									.set(ProductPrice.column_starttime, new Date()).save();
								}
							}
						}
					}
				}*/
				for(ProductPrice pp : productPrices){
					
					if(!"null".equals(pp.getPKValue())){
						//禁用之前价格
						ProductPrice.dao.findById(pp.getPKValue()).set(ProductPrice.column_status, 0).set(ProductPrice.column_disabletime, new Date()).update();
					}
					if(pp.getFloat(ProductPrice.column_outPrice)==null||pp.getFloat(ProductPrice.column_outPrice).equals("null")){
						pp.set(ProductPrice.column_outPrice, 0.0);
					}
					pp.set(ProductPrice.column_priceIds, price.getPKValue())
					.set(ProductPrice.column_starttime, new Date()).save();
				}
				
			}
		}
		result.setCode(ConstantRender.render_success_code);
		return result;
	}

}
